package com.wn.common.dialog

import android.view.Gravity
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.fragment.app.FragmentActivity
import com.blankj.utilcode.util.ConvertUtils
import com.wn.common.databinding.DialogPickerCalendarBinding
import com.wn.framework.base.BaseDialog
import com.wn.framework.base.BaseDialogFragment
import com.wn.framework.ext.onClick
import com.wn.framework.utils.ViewUtils.setClipViewCornerTopRadius
import java.text.SimpleDateFormat
import java.util.*

/**
 * @Author: wn
 * @Date: 2025/8/1 17:05
 * @Description: 生日日期弹框 -- setBirthDayDateCall 方法中 传入 lambda 中 获取 格式化的 日期
 */
class SelectBirthdayDialog {
  class Builder(activity: FragmentActivity) : BaseDialogFragment.Builder<Builder>(activity) {

    private val mCurrentData = Calendar.getInstance()

    private var mOnDateCall: ((String?) -> Unit)? = null

    /**
     * 此处为实例 的 XML 布局
     */
    private val mBinding: DialogPickerCalendarBinding =
      DialogPickerCalendarBinding.inflate(LayoutInflater.from(activity))

    init {
      initView()
    }

    private fun initView() {
      // 绑定 实例 布局
      setContentView(mBinding.root)
      setWidth(ViewGroup.LayoutParams.MATCH_PARENT)
      setHeight(ViewGroup.LayoutParams.WRAP_CONTENT)
      setAnimStyle(BaseDialog.AnimStyle.BOTTOM)
      gravity = Gravity.BOTTOM

      setClipViewCornerTopRadius(mBinding.clRoot, ConvertUtils.dp2px(12.0F))
      mBinding.calendarView.setOnDateChangeListener { _, year, month, dayOfMonth ->
        mCurrentData.set(year, month, dayOfMonth)
      }
      // 点击 完成 调用 传入的  lambda  获取 日期
      mBinding.tvComplete.onClick {
        val timeStamp = SimpleDateFormat("yyyy-MM-dd", Locale.CHINA).format(mCurrentData.time)
        mOnDateCall?.invoke(timeStamp)
        dismiss()
      }
      mBinding.tvCancel.onClick {
        dismiss()
      }
    }

    /**
     * 获取 格式化的 日期
     * @param onDateCall Function1<String?, Unit>
     * @return Builder
     */
    fun setBirthDayDateCall(onDateCall: ((String?) -> Unit)): Builder {
      mOnDateCall = onDateCall
      return this
    }
  }
}